﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using eeenno.ReaderEngine.Adapters;

namespace eeenno.ReaderEngine.Utilities
{
    /// <summary>
    /// Manages all application update functionality
    /// </summary>
    public static class UpdateManager
    {
        private static string WEB_UPDATE_FILE = @"http://emuneee.com/eeenno/SpeeedReader/update/SpeeedReader.xml";
        private static string CAB_FILE_NAME = "speeedreader.cab";

        private static int APP_NAME_INDEX = 0;
        private static int VERSION_INDEX = 1;
        private static int CAB_URL_INDEX = 2;

        /// <summary>
        /// Queries server to determine if an application update is available
        /// </summary>
        /// <returns>version of update</returns>
        public static float IsUpdateAvailable()
        {
            HttpWebRequest update_avail_request = WebManager.GetWebRequest(WEB_UPDATE_FILE, false);
            string update_info = WebManager.GetResponseStreamData(update_avail_request);

            if (update_info != string.Empty)
            {
                XmlDocument update_xml = XmlEngine.GetXmlDocument(update_info);
                float new_version = 0.0f;
                float current_version = 0.0f;
                try
                {
                    new_version = float.Parse(update_xml.ChildNodes[0].ChildNodes[VERSION_INDEX].InnerText);
                    current_version = float.Parse(SpeeedReader.MajorVersion() + "." + SpeeedReader.MinorVersion());
                }
                catch (Exception e)
                {
                    return -1;
                }

                if (new_version > current_version)
                {
                    return new_version;
                }
            }
            
            return -1;
        }

        /// <summary>
        /// Downloads the updated CAB file
        /// </summary>
        /// <returns>file name and path to downloaded file</returns>
        public static string DownloadUpdate()
        {
            HttpWebRequest update_avail_request = WebManager.GetWebRequest(WEB_UPDATE_FILE, false);
            string update_info = WebManager.GetResponseStreamData(update_avail_request);

            if (update_info != string.Empty)
            {
                XmlDocument update_xml = XmlEngine.GetXmlDocument(update_info);
                string cab_url = update_xml.ChildNodes[0].ChildNodes[CAB_URL_INDEX].InnerText;
                HttpWebRequest download_request = WebManager.GetWebRequest(cab_url, false);
                string filepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
                    + @"\" + CAB_FILE_NAME;
                if (WebManager.DownloadFile(download_request, filepath))
                {
                    return filepath;
                }
            }
            return string.Empty;
        }
    }
}
